package com.fzkj.juc.threadlocal;

import java.util.concurrent.*;

/**
 * @DESCRIPTION
 */
public class ThreadPoolTest {
    public static void main(String[] args) {
//        ExecutorService poolExecutor = Executors.newCachedThreadPool();
//        Executors.newFixedThreadPool(5);
//        Executors.newCachedThreadPool();
        mExecutor();
    }

    private static void mExecutor() {
        /**
         * public ThreadPoolExecutor(int corePoolSize, // 核心线程数
         *                               int maximumPoolSize, // 最大线程数
         *                               long keepAliveTime, // 活跃时间
         *                               TimeUnit unit, // 活跃时间单位
         *                               BlockingQueue<Runnable> workQueue,  // 阻塞队列
         *                               ThreadFactory threadFactory, // 线程生产工厂
         *                               RejectedExecutionHandler handler) { // 拒绝策略
         */
        ExecutorService poolExecutor = new ThreadPoolExecutor(
                2,
                5,
                1L,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(5),
                Executors.defaultThreadFactory(),
//                new ThreadPoolExecutor.AbortPolicy());
//                new ThreadPoolExecutor.CallerRunsPolicy());
//                new ThreadPoolExecutor.DiscardOldestPolicy());
                new MyRejectedPolicy());

        try {
            for (int i = 0; i < 11; i++) {
                poolExecutor.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + "\t 处理业务...");
                });
            }
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            poolExecutor.shutdown();;
        }
    }
}

